window.onload = function () {
	// 开局刷新
	if (location.href.indexOf("#reloaded") == -1) {
		location.href = location.href + "#reloaded"
		location.reload()
	}
	// 顶栏点击事件
	const a = document.querySelector(".topPreset a")
	a.addEventListener("touchstart", function () {
		history.back()
	})
	//   异常时间事件
	const abnormal = document.querySelector(".content1 .times .right")

	abnormal.addEventListener("click", function () {})
	//   上报位置
	const address = document.querySelector(".content1 .address .right ")
	address.addEventListener("click", function () {})
	//   异常类型
	const kind = document.querySelector(".content1 .kind .right ")
	//   类型选择
	// 拿到所有复选框
	const inputs = document.querySelectorAll(".categories input")
	const inpu = document.querySelector(".t1")
	// console.log(inputs[1].checked);
	let inputsStr = ""
	// for (let i = 0; i < inputs.length; i++) {
	//   if (inputs[i].checked === true) {
	//     inputsStr += inputs[i].title;
	//     console.log(inputs[i].title);
	//   }

	function getinputsText() {
		//每次默认为空
		inputsStr = ""
		for (let i = 0; i < inputs.length; i++) {
			if (inputs[i].checked === true) {
				inputsStr += inputs[i].title + " ;"
				// console.log(inputs[i].title);
			}
		}

		// inputs.forEach((item, index) => {});
		// console.log(item[index].title);
		return inputsStr
	}
	// console.log(getinputsText());
	getinputsText()
	// console.log(inputsStr);
	const categories = document.querySelector(".categories")
	let flag = false
	kind.addEventListener("click", function () {
		if (!flag) categories.style.display = "block"
	})
	const Cancel = document.querySelector(".categories .types span")
	Cancel.addEventListener("click", function () {
		kind.innerHTML = `请选择
    <span class="iconfont icon-xiangyou1"></span>`
		for (let i = 0; i < inputs.length; i++) {
			inputs[i].checked = false
		}
		if (!flag) categories.style.display = "none"
	})

	// 异常点点变量
	let dataDress = ""
	// 异常类型变量
	let dataType = ""
	// 点击确认提交

	const btn = document.querySelector(".categories button")
	btn.addEventListener("click", function () {
		kind.innerHTML =
			getinputsText() ||
			`请选择
    <span class="iconfont icon-xiangyou1"></span>`
		dataType = kind.innerText

		categories.style.display = "none"
	})

	// 获取时间
	$("#kinerDatePickerInput1").kinerDatePicker({
		clickMaskHide: true,
		showHandler: function (ctx) {
			// console.log("显示时间选择器:", ctx);
		},
		hideHandler: function (ctx) {
			// console.log("隐藏时间选择器:", ctx);
		},
		changeHandler: function (vals, ctx) {
			// console.log("时间改变:", vals, ctx);
		},
		okHandler: function (vals, ctx) {
			// console.log("确定选择:", vals, ctx);
		},
		cancelHandler: function (ctx) {
			// console.log("取消选择:", ctx);
		}
	})
	$("#btn1").click(function () {
		// console.log($("#kinerDatePickerInput1").kinerDatePickerVal());
	})
	$("#kinerDatePickerInput2").kinerDatePicker({
		clickMaskHide: true,
		showHandler: function (ctx) {
			// console.log("显示时间选择器:", ctx);
		},
		hideHandler: function (ctx) {
			// console.log("隐藏时间选择器:", ctx);
		},
		changeHandler: function (vals, ctx) {
			// console.log("时间改变:", vals, ctx);
		},
		okHandler: function (vals, ctx) {
			// console.log("确定选择:", vals, ctx);
		},
		cancelHandler: function (ctx) {
			// console.log("取消选择:", ctx);
		}
	})

	$("#btn2").click(function () {
		// console.log($("#kinerDatePickerInput2").kinerDatePickerVal());
	})
	// 绑定时间点击事件
	const abnormalTimes = document.querySelector(".content1 .times .right ")
	const contentTime = document.querySelector(".content1 .abnormalTime .cotent")
	// 时间值
	const kinerDatePickerInput = document.querySelector(
		".content1 .abnormalTime .kinerDatePickerInput"
	)
	const btnCancel = document.querySelector(".content1 .abnormalTime .Cancel")
	const btnconfirm = document.querySelector(".content1 .abnormalTime .confirm")
	let flagTime = false
	let flagscroll = false
	//获取时间
	let datatime = ""

	abnormalTimes.addEventListener("click", function () {
		flagTime = true
		if (flagTime) {
			flagscroll = true
			abnormalTimes.style.opacity = "0"
			contentTime.style.display = "block"
		}
	})

	// 点击取消选择
	btnCancel.addEventListener("click", function () {
		if (flagTime) {
			flagscroll = false
			kinerDatePickerInput.innerText = "请选择时间"
			abnormalTimes.style.opacity = "1"
			contentTime.style.display = "none"
		}
	})
	const dataAbn = { times: "", address: "", type: "", datail: "" }
	// 点击确认选择
	btnconfirm.addEventListener("click", function () {
		datatime = kinerDatePickerInput.innerText + "  13:00"
		abnormalTimes.style.opacity = "1"
		contentTime.style.display = "none"
		abnormalTimes.innerText = datatime
		dataAbn.times = datatime
	})
	dataAbn.address = "浙江省 杭州市 钱塘区"
	//监视页面滑动
	const content1 = document.querySelector(".content1")
	window.addEventListener("scroll", function () {
		const top = this.document.documentElement.scrollTop
		// console.log(top);
		if (flagscroll) {
			if (top > content1.offsetTop) {
				abnormalTimes.style.opacity = "1"
				contentTime.style.display = "none"
			} else {
				abnormalTimes.style.opacity = "0"
				contentTime.style.display = "block"
			}
		} else return
	})
	// 异常详情变量
	let dataDatails = ""
	// 渲染输入字数
	const abnormalText = document.querySelector("#textAbnor")
	const abnormalNum = document.querySelector("#num")
	// 提交条件阀门！！！
	let btnFlag = false
	// 绑定点击事件
	let length = 0
	let texts = ""
	const bottom = document.querySelector(".btn")
	abnormalText.addEventListener("input", function () {
		const active = document.querySelector(".btn.active")
		texts = abnormalText.value.trim()
		length = texts.length
		if (length >= 1) {
			btnFlag = true
		} else {
			btnFlag = false
		}
		if (btnFlag) {
			bottom.classList.add("active")
			bottom.disabled = false
		} else {
			bottom.disabled = true
			active && active.classList.remove("active")
		}
		dataDatails = abnormalText.value
		abnormalNum.innerText = length
	})
	// 点击加载图片上传图片
	const ul1 = document.querySelector(".voucher ul")
	const ul2 = document.querySelector(".goodsImag ul")
	const btnfirn = document.querySelector(".voucher .firm")
	btnfirn.addEventListener("click", function () {
		renderImg(ul1)
	})
	function renderImg(ul) {
		//取到n~m的随机整数
		function getRandom(n, m) {
			return Math.floor(Math.random() * (m - n + 1)) + n
		}
		//调用
		let random = getRandom(1, 3)

		const li = document.createElement("li")
		li.innerHTML = `
     <li><img src="./image/收据${random}.png" alt>
     </li>`
		ul.append(li)
	}
	// 提交
	const form = document.querySelector("form")

	const goIn = "../module04/mainFunctionPage.html"
	form.addEventListener("submit", function (e) {
		e.preventDefault()
		dataAbn.type = dataType
		dataAbn.datail = dataDatails
		// console.log(btnFlag);
		const dataAbnormal = []
		dataAbnormal.push(dataAbn)
		// 数据上传到本地
		localStorage.setItem("dataAbnormal", JSON.stringify(dataAbnormal))
		location.href = "Details-abnormal.html"
	})
}
